﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using HelloMinasan.Business;
using HelloMinasan.Domain;
using System.ComponentModel;
using HelloMinasan.Resources;
namespace HelloMinasan.Web.ViewModels
{
    public class LogInModel
    {
        public LogInModel()
        {
            ReturnUrl = "/MemberProfile";
            RememberMe = true;
            Status = UserStatus.Active;
        }
        
        [Display(Name = "Common_YourEmailAddress", ResourceType = typeof(Resource))]
        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "HomeLogin_EmailEmpty")]
        [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        [RegularExpression(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "EmailInvalid")]
        public string Email { get; set; }

        [StringLength(50)]
        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "HomeLogin_PasswordEmpty")]
        [DataType(DataType.Password)]
        [Display(Name = "Common_YourPassword", ResourceType = typeof(Resource))]
        public string Password { get; set; }

        [Display(Name = "Remember me(up to 30 days)")]
        public bool RememberMe { get; set; }

        public string Captcha { get; set; }

        public int LoginFailCount { get; set; }

        public UserStatus Status { get; set; }

        public string ReturnUrl { get; set; }
    }
    public class UserValidateController : Controller
    {
        private IUserService userService;
        public UserValidateController(IUserService userService)
        {
            this.userService = userService;
        }
        public JsonResult IsValidUser(string userName)
        {
            User user = userService.FindAll<User>(x => x.Username == userName).FirstOrDefault();
            if (user == null)
            {
                ModelState.AddModelError("invaliduser", "invaliduser");
                return Json(Resources.Resource.InvalidObject, JsonRequestBehavior.AllowGet);
            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }

    }
}